<?php

namespace app\api\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\UserCoupons as UserCouponsModel;

class UserCoupons extends UserCouponsModel
{
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }
//    public function getStartTimeAttr($value)
//    {
//        return $value ? date('Y-m-d H:i:s', $value) : '';
//    }
//    public function getEndTimeAttr($value)
//    {
//        return $value ? date('Y-m-d H:i:s', $value) : '';
//    }

    /**
     * 优惠券领取。购买数量
     */
    public static function getCountByCouponId($user_id, $coupon_id)
    {
        return self::where('user_id', $user_id)->where('coupon_id', $coupon_id)->count();
    }

    public static function getUserCoupon($user_id, $coupon_id)
    {
        return self::where('user_id', $user_id)->where('coupon_id', $coupon_id)->find();
    }

    public static function createUserCoupon($user_id, $coupon, $way = false)
    {
        //生成优惠码
        $code = strtoupper(substr(md5(date("YmdHis").mt_rand(10000, 99999).$user_id), 8, 16));
        //生成二维码
        $qrcode = Qrcode::createQrcode('youhuima'. $code);

        return self::create([
            'user_id' => $user_id,
            'coupon_id' => $coupon->id,
            'service_id' => $coupon->service_id,
            'code' => $code,
            'qrcode' => $qrcode,
            'coupon_name' => $coupon->title,
            'discount_amount' => $coupon['type'] == 4 ? $coupon->line_money : $coupon->discount_amount,
            'need_amount' => $coupon->need_amount,
            'start_time' => $coupon->expired_type == 2 ? time() : $coupon->start_time,
            'end_time' => $coupon->expired_type == 2 ? time() + $coupon->expiry_date * 86400 : $coupon->end_time,
            'type' => $coupon->discount_type,
            'way' => $way ? 3 : ($coupon['money'] > 0 ? 1 : 2),
            'buying_amount' => $coupon->money,
            'coupon_type' => $coupon['type'],
            'createtime' => time()
        ]);
    }

    public static function getUserCoupons($user_id, $time = 0, $design_service_id = 0, $length = 15)
    {
        $where = [
            'user_id' => ['=', $user_id]
        ];
        if ($time) {
            $service_id = DesignServices::where('id', $design_service_id)->value('service_id');
            $price = DesignServices::getPriceById($design_service_id) ?: 0;
            $where['end_time'] = ['>', $time];
            $where['status'] = ['=', 1];
            $where['coupon_type'] = ['<', 3];
            $coupons = self::where($where)->whereIn('service_id', [0, $service_id])->where('need_amount', ['=', 0], ['<=', $price], 'or')->order("createtime desc")->paginate($length);
        } else {
            $coupons = self::where($where)->order("createtime desc")->paginate($length);
        }
        if ($coupons->items()) {
            foreach ($coupons->items() as $val) {
                if ($val['end_time'] < time() && $val['status'] == 1) {
                    $val->status = 0;
                }
                $val['start_time'] = $val['start_time'] ? date('Y-m-d', $val['start_time']) : '';
                $val['end_time'] = $val['end_time'] ? date('Y-m-d', $val['end_time']) : '';
            }
        }
        return $coupons;
    }

    public static function getCouponCount($user_id, $time, $design_service_id)
    {
        $service_id = DesignServices::where('id', $design_service_id)->value('service_id');
        $price = DesignServices::getPriceById($design_service_id) ?: 0;
        $where = [
            'user_id' => ['=', $user_id],
            'end_time' => ['>', $time],
            'status' => ['=', 1]
        ];
        return self::where($where)->whereIn('service_id', [0, $service_id])->where('need_amount', ['=', 0], ['<=', $price], 'or')->count();
    }

    public static function getCouponPrice($user_id, $coupon_id)
    {
        return self::where('user_id', $user_id)->where('coupon_id',$coupon_id)->value('discount_amount');
    }

    public static function getByCode($code)
    {
        return self::where('code', $code)->find();
    }

}
